import requests
import re
import time
import random
def pa(url,name):
    #爬取steam
    f1=open("httpreq.txt","r")
    t1=f1.read().split("\n")
    f1.close()
    t2=[]
    headers={}
    for i in t1[1:]:
        try:
            headers[i.split(":")[0]]=i.split(":")[1].strip(" ")
        except:
            ''
    proxy ='127.0.0.1:1080'
    proxies = {"http": "http://%(proxy)s/" % {'proxy': proxy},"https": "http://%(proxy)s/" % {'proxy': proxy}}
    s = requests.session()
    req = s.get(url,headers=headers,proxies=proxies)
    if(str(req)=="<Response [200]>"):
        print("回包已抓取")
    else:
        print("你的包有可能出现了问题,异常代码:")
        print(req)
    f1=open("req.txt","w",encoding="UTF-8")
    req=req.text
    f1.write(req)
    f1.close()
    
def ql(zu):
    #清理空行
    try:
        last=1
        with open("jgb"+zu+".txt","r") as f:
            ff=f.readlines()
        fg=[]
        for i in ff:
            fg.append(i.strip("\n"))
        ff=open("jgb"+zu+".txt","w")
        for i in range(len(fg)):
            if(len(fg[i])>1):
                ff.write(fg[i]+"\n")
        ff.close()
        print("空行已清理")
    except:
        ''
    
def jiexi():
    #正则过滤爬取内容
    f1=open("req.txt","r",encoding="UTF-8")
    f1txt=f1.read()
    f1.close()
    pattern = re.compile(r'(?<=>)[^\n].+\d*(?=</span>)')
    a=0
    b=2
    jg=[]
    for i in pattern.findall(f1txt)[30:]:
        if i !="Counter-Strike: Global Offensive":
            if "¥" in i:
                a+=1
                if a==2:
                    a=0
                    continue
            if b==2:
                b=0
                continue
            if "&" in i:
                break  
            jg.append(i)
            b+=1
    return jg
            
def jijia(zu,jg,na):
    #价格自动统计
    zu=zu.upper()
    jgb=["no","no","no","no","no"]
    na=na.split("|")[1].strip(" ")
    for k in jg:
        if na in k:
            if "StatTrak" in k:
                continue
            if "破损" in k:
                jgb[0]=(float(jg[jg.index(k)-1].strip("¥ ")))
            if "战痕" in k:
                jgb[1]=(float(jg[jg.index(k)-1].strip("¥ ")))
            if "久经" in k:
                jgb[2]=(float(jg[jg.index(k)-1].strip("¥ ")))
            if "略有" in k:
                jgb[3]=(float(jg[jg.index(k)-1].strip("¥ ")))
            if "崭新" in k:
                jgb[4]=(float(jg[jg.index(k)-1].strip("¥ ")))
    else:
        jgb.insert(0,jg[1].replace(" ","").strip(" ").split("(")[0])
    #寻找最新标签
    try:
        with open("jgb"+zu+".txt","r") as f:
            last=int(list(f.readlines()[-1])[1])+1
    except:
        last=1
    txt=zu+str(last)+"="+str(jgb)+"\n"
    txt=txt.replace("'","\"").replace(", ",",").replace(" ","-")
    return txt,jgb


def xieru(zu,jgb,txt):
    xgm=0
    try:
        f12=open("jgb"+zu+".txt","r")
        f1=f12.readlines()
        f12.close()
    except:
        f12=open("jgb"+zu+".txt","wb")
        f12.close()
        f1=[]
    
    #寻找曾是否出现
    for dqz in f1:
        if jgb[0] in dqz:
            txt=txt.replace(txt.split("=")[0],dqz.split("=")[0]).strip("\n")
            f1[f1.index(dqz)]=txt
            f13=open("jgb"+zu+".txt","w")
            for i in f1:
                f13.write(i.strip("\n")+"\n")
            f13.close()
            xgm=1

    return xgm


def new(zu,txt):
    f12=open("jgb"+zu+".txt","a")
    f12.write(txt)
    f12.close()           


#自定义配方组
pq=input("是否开始爬取数据?(回车继续,输入F退出):").upper()
if(pq!="F"):   
    if(input("是否需要进行批量爬取?(T/F，默认不启用)").upper()=="T"):
        while True:
            zu=input("请输入需要批量爬取的组(不知道就写A,自定义写sr):").upper()
            if(zu==" "):
                continue
            break
        f1=[]
        if(zu=="SR"):
            zu=input("请输入你要添加进的组(不知道就写A):")#写自定义批量爬取
            while True:
                a="=\""+input("输入你需要爬取的名称，输入BYE(大写)退出(例:AUG|琥珀冲流):")
                if(a=="=\"BYE"):
                    break
                if(a=="=\""):
                    continue
                f1.append(a)
        else:
            try:
                with open("jgb"+zu+".txt","r") as f:
                    f1=f.readlines()
            except:
                print("请检查组名是否有误！")
        input("确保httpreq.txt为最新以后按Enter继续...")
        hhs=len(f1)
        print(hhs)
        input()
        xxs=1
        for i in f1:
            na=i.split("=")[1].split("\"")[1]
            url="https://steamcommunity.com/market/search?appid=730&q="+na
            #try:
            pa(url,na)
            print(na+"爬取已成功")
            #except:
              #  print("爬取失败,请检查headers是否可用，或者ssr是否正常开启！")
               # quit()
            try:
                jg=jiexi()
                na=str(jg[1].split("|")[0].strip(" ").split("(")[0]+"|"+jg[1].split("|")[1].strip(" ").split("(")[0]).strip(" ")
                print(na+"价格已解析")
            except:
                print("文件解析失败,请检查headers数据是否正确")
                quit()
            try:
                txt,jgb=jijia(zu,jg,na)
                if "no" in txt:
                    print("=====================================================================")
                    print("注意:该条目搜索含未知数额，可能是搜索结果无该物品磨损价格或无磨损物品")
                    print("=====================================================================")
                print(na+"价格已计算")
            except:
                print("计算失败,请检查headers数据是否正确")
                quit()
            try:
                ql(zu)
                k=xieru(zu,jgb,txt)
                if(k==0):
                    print(na+"无此条目，正在创建新条目")
                    new(zu,txt)
                    print(na+"内容已写入")
                else:
                    print(na+"内容已写入")
            except:
                print("写入失败，请检查软件权限是否有问题")
                quit()
            if(hhs!=xxs):
                xxs+=1
                #代码休息
                sj=random.randint(29,37)#调整当前位置修改爬取间隔时间
                print("【防封】:"+str(sj)+"秒后继续爬取")
                time.sleep(sj)#注释该行取消爬取间隔
            print("================================")
    else:
        while True:
            zu=input("请输入合成组名(不知道请填A):").upper()
            if(zu!=""):
                break
        zwurl=input("请输入你需要爬取的枪械名称(例:AUG|琥珀冲流):")
        url="https://steamcommunity.com/market/search?appid=730&q="+zwurl
        input("确保httpreq.txt为最新以后按Enter继续...")
        try:
            pa(url,zwurl)
            print("爬取已成功")
        except:
            print("爬取失败,请检查headers是否可用，或者ssr是否正常开启！")
            quit()
        try:
            jg=jiexi()
            zwurl=str(jg[1].split("|")[0].strip(" ").split("(")[0]+"|"+jg[1].split("|")[1].strip(" ").split("(")[0]).strip(" ")
            print(zwurl+"价格已解析")
        except:
            print("文件解析失败,请检查headers数据是否正确")
            quit()
        try:
            txt,jgb=jijia(zu,jg)
            print(zwurl+"价格已计算")
        except:
            print("计算失败,请检查headers数据是否正确")
            quit()
        try:
            ql(zu)
            a=xieru(zu,jgb,txt)
            print(zwurl+"内容已写入")
            if(a==0):
                d=input("未找到相同值，是否写入新条目？(取消输入F):").upper()
                if(d!="F"):
                    try:
                        new(zu,txt)
                        print(zwurl+"项目已生成")
                    except:
                        print("新条目写入失败，请检查文件是否正常")
                        quit()
        except:
            print("写入失败，请检查软件权限是否有问题")
            quit()
    input("点击回车继续...")
    




